package com.example.demo.controller;

import com.example.demo.common.Constants;
import com.example.demo.config.JsonUtil;
import com.example.demo.config.redis.RedisHandler;
import com.example.demo.entity.BaseResult;
import com.example.demo.entity.Person;
import com.example.demo.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author: 吕祥
 * @Date: Created in 15:45 2017/10/10/010
 */
@RestController
@RequestMapping("/redis/person")
public class PersonRedisController {

    @Autowired
    private PersonService personService;

    @Autowired
    private RedisHandler redisHandler;

    /**
     * 新增
     * @param name 姓名
     * @param sex   性别
     * @param age   年龄
     * @return
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public BaseResult add(@RequestParam String name, @RequestParam String sex, @RequestParam Integer age) {
        boolean b = personService.addRedis(name, sex, age);
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, b);
    }

    /**
     * 修改
     * @param id    主键id
     * @param name  姓名
     * @param sex   性别
     * @param age   年龄
     * @return
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public BaseResult update(@RequestParam String id, @RequestParam String name, @RequestParam String sex, @RequestParam Integer age) {
        boolean b = personService.updateRedis(id, name, sex, age);
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, b);
    }

    /**
     * 删除
     * @param id    主键id
     * @return
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public BaseResult delete(@RequestParam String id) {
        boolean b = personService.deleteRedis(id);
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, b);
    }

    /**
     * 根据id查询
     * @param id    主键id
     * @return
     */
    @RequestMapping(value = "findOne", method = RequestMethod.POST)
    public BaseResult findOne(@RequestParam String id) {
        Map<String,Person> map = personService.findOneRedis(id);
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, map);
    }

    /**
     * 查询所有
     * @return
     */
    @RequestMapping(value = "findAll", method = RequestMethod.POST)
    public BaseResult findAll() {
        return personService.findAllRedis();
    }

    /**
     * 测试Set和zSet区别
     * @param str
     * @return
     */
    @RequestMapping(value = "testSet", method = RequestMethod.POST)
    public BaseResult test(String str) {
        String redisKey = Constants.REDISKEY;
        boolean b = false;
        //Set类型
        //缓存中是无序的
        b = redisHandler.getLocalRedisTemplate().cacheSet(redisKey,"testSet", JsonUtil.toJson(str));
        //ZSet类型
        //缓存中是有序的 1-9, A-Z, a-z
        b = redisHandler.getLocalRedisTemplate().cacheZSet(redisKey,"testZSet", JsonUtil.toJson(str));
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, b);
    }
}
